package trungnt.wipeoff.model;

import trungnt.wipeoff.Cs;
import trungnt.wipeoff.MenuState;
import trungnt.wipeoff.PlayState;
import trungnt.wipeoff.enums.ButtonEnum;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.BitmapFont.TextBounds;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;

public class ButtonActor extends Actor {

	private GameState _gameState;
	// private PlayState _playState;

	private BitmapFont bitmapFont;

	public ButtonEnum buttonEnum;
	private String buttonText;

	public ButtonActor(MenuState menuState, ButtonEnum value) {
		this._gameState = menuState;
		this.buttonEnum = value;

		this.bitmapFont = this._gameState.getGame().getAssetManager().get("fonts/bmp/diavlo_black_72.fnt", BitmapFont.class);
		this.bitmapFont.setColor(Color.WHITE);

		this.init();
	}

	public ButtonActor(PlayState playState, ButtonEnum value) {
		this._gameState = playState;
		this.buttonEnum = value;

		this.bitmapFont = this._gameState.getGame().getAssetManager().get("fonts/bmp/diavlo_bold_60.fnt", BitmapFont.class);
		this.bitmapFont.setColor(Color.WHITE);

		this.init();
	}

	private void init() {
		TextBounds tb = null;
		float x = 0;
		float y = 0;

		switch (this.buttonEnum) {
			case NEWGAME:
				this.buttonText = "NEW GAME";
				tb = this.bitmapFont.getBounds(this.buttonText);

				x = Cs.VW / 2 - tb.width / 2;
				y = 817 + tb.height / 2;

				break;

			case RANKING:
				this.buttonText = "RANKING";
				tb = this.bitmapFont.getBounds(this.buttonText);

				x = Cs.VW / 2 - tb.width / 2;
				y = 696 + tb.height / 2;

				break;

			case HOWTOPLAY:
				this.buttonText = "HOW TO PLAY";
				tb = this.bitmapFont.getBounds(this.buttonText);

				x = Cs.VW / 2 - tb.width / 2;
				y = 575 + tb.height / 2;

				break;
			case EXIT:
				this.buttonText = "EXIT";
				tb = this.bitmapFont.getBounds(this.buttonText);

				x = Cs.VW / 2 - tb.width / 2;
				y = 454 + tb.height / 2;

				break;
			case REPLAY:
				this.buttonText = "replay";
				tb = this.bitmapFont.getBounds(this.buttonText);

				x = 159;
				y = 523;

				break;
			case MENU:
				this.buttonText = "menu";
				tb = this.bitmapFont.getBounds(this.buttonText);

				x = 407;
				y = 523;

				break;
		}

		this.setBounds(x, y - tb.height, tb.width, tb.height);

		this.addListener(new InputListener() {
			@Override
			public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
				// Do something
				Gdx.app.log("TrungNT", "Button was touchUp: " + x + " - " + +y);
				_gameState.startAnimation(buttonEnum);
			}

			@Override
			public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
				Gdx.app.log("TrungNT", "Button was touchDown: " + x + " - " + +y);
				return true;
			}
		});
	}

	@Override
	public void draw(SpriteBatch batch, float parentAlpha) {
		this.bitmapFont.draw(batch, this.buttonText, this.getX(), this.getY() + this.getHeight());
	}

	public void dispose() {
		this.bitmapFont.dispose();
	}

	public boolean isTouched(float x, float y) {
		if (x >= this.getX() && x <= this.getX() + this.getWidth() && y >= this.getY() && y <= this.getY() + this.getHeight()) {
			return true;
		}
		else {
			return false;
		}
	}
}
